<script>
	import {mapMutations} from 'vuex'
	import {downloadFilePromise} from '@/utils/upload.js'
	export default {
		onLaunch: function() {
			// 初始化userInfo
		  const	userInfo = {
				avatarUrl:'',
				id:'',
				identity:'',
				nickName:'',
				phone:'',
				token:'',
			}
			const value = uni.getStorageSync('userInfo');
			userInfo.avatarUrl = value.avatarUrl
			userInfo.nickName = value.nickName
			this.initUser(userInfo)
			this.getRestaurant()
			this.getSwiperList()
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 获取vuex中的mutation
			...mapMutations(['initRestaurant','initUser','initSwiperList']),
			// 获取餐厅信息
			async	getRestaurant () {
				if(!this.$store.getters.restaurant.id) {
				  const result = await this.$u.api.getRestaurant()
					const requestList = []
					result.navCategory?.forEach(item => {
						item.food?.forEach(food => {
							if (food.foodPicture.length > 0) {
								requestList.push(downloadFilePromise(food.foodPicture[0].url).then(res => {
									food.foodPicture[0].url = res
								}))
							}
						})
					})
					await Promise.all(requestList)
					if (result.pictureList && result.pictureList.length > 0 && result.pictureList[0].url) {
						result.pictureList[0].url = await downloadFilePromise(result.pictureList[0].url)
					}
					this.initRestaurant(result)
				}
			},
			async getSwiperList () {
				const result =await this.$u.api.getSwiper()
				const changeResultUrl = async () => {
					const requestList = []
					for(let i = 0; i < result.length; i++) {
						requestList.push(
							downloadFilePromise(result[i].url).then(res => {
								result[i].url = res
							})
						)
					}
					await Promise.all(requestList)
				}
				await changeResultUrl()
				this.initSwiperList(result)
			}
		}
	}
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "@/uni_modules/uview-ui/index.scss";
</style>
